/*
 * report_ComAnulados.java
 *
 * Created on 31 de agosto de 2008, 11:10 PM
 */

package administrador;
import net.sf.jasperreports.engine.JRException;
import java.util.Calendar;

/**
 *
 * @author  Jorge
 */
public class report_ComAnulados extends javax.swing.JFrame {

    /** Creates new form report_ComAnulados */
    Conexion conexion;
    calendario.JCalendar jCalendar = null;
    String fechaInicial="";
    public report_ComAnulados(Conexion conexion) {
        this.conexion=conexion;
        initComponents();
        setVisible(true);
         jCalendar = new calendario.JCalendar(this, "Calendario", true);
         fechaInicial=new java.sql.Date(System.currentTimeMillis()).toString();
         txt_inicio.setText(fechaInicial);
         txt_fin.setText(fechaInicial);
         fecha1=new java.sql.Date(System.currentTimeMillis());
         fecha2=new java.sql.Date(System.currentTimeMillis());
    }
        java.sql.Date fecha1=null;
    java.sql.Date fecha2=null;
    public void sacarFecha(int opcion) {
        Calendar cDate = jCalendar.getCalendar();
        if (cDate == null) {
            cDate = Calendar.getInstance();
        }
        jCalendar.setCalendar(cDate);
        jCalendar.setVisible(true);
        if (jCalendar.isOkPressed()) {
            if (opcion == 1) {
                fecha1 = new java.sql.Date(jCalendar.getDate().getTime());
                System.out.println(fecha1);
                txt_inicio.setText(fecha1.toString());
            } else if (opcion == 2) {
                fecha2 = new java.sql.Date(jCalendar.getDate().getTime());
                txt_fin.setText(fecha2.toString());
            }
            jCalendar.setVisible(false);
        }
    }
    public void imprimir(String nombreArchivo){
        String path = "./Reportes/";
        java.util.Map parametros = new java.util.HashMap();
        if (cmb_comprobante.getSelectedItem().equals("Factura")){
            parametros.put("tipo_Comprobante","F");
            parametros.put("fechaInicio",fecha1);
            parametros.put("fechafin",fecha2);
        }else if(cmb_comprobante.getSelectedItem().equals("Nota de Venta")){
            parametros.put("tipo_Comprobante","N");
            parametros.put("fechaInicio",fecha1);
            parametros.put("fechafin",fecha2);
        }else if(cmb_comprobante.getSelectedItem().equals("Todos")){
            parametros.put("tipo_Comprobante","%");
            parametros.put("fechaInicio",fecha1);
            parametros.put("fechafin",fecha2);
        }
        try {
                generaReporte(path+ nombreArchivo,parametros);
        } catch (Exception e) {
                System.out.println(e.getMessage());
        }  
    }
    public void  generaReporte(String path,java.util.Map parametros) throws JRException{
        net.sf.jasperreports.engine.JasperReport report = net.sf.jasperreports.engine.JasperCompileManager.compileReport(path);
        net.sf.jasperreports.engine.JasperPrint print = net.sf.jasperreports.engine.JasperFillManager.fillReport(report, parametros, conexion.getconexion());
        net.sf.jasperreports.view.JasperViewer.viewReport(print,false);
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jButton3 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        txt_inicio = new javax.swing.JTextField();
        txt_fin = new javax.swing.JTextField();
        cmb_comprobante = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(administrador.AdministradorApp.class).getContext().getResourceMap(report_ComAnulados.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.setLayout(null);

        jButton3.setIcon(resourceMap.getIcon("jButton3.icon")); // NOI18N
        jButton3.setName("jButton3"); // NOI18N
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton3);
        jButton3.setBounds(250, 40, 26, 21);

        jButton2.setIcon(resourceMap.getIcon("jButton2.icon")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton2);
        jButton2.setBounds(250, 10, 26, 25);

        txt_inicio.setEditable(false);
        txt_inicio.setName("txt_inicio"); // NOI18N
        jPanel1.add(txt_inicio);
        txt_inicio.setBounds(90, 10, 158, 20);

        txt_fin.setEditable(false);
        txt_fin.setName("txt_fin"); // NOI18N
        jPanel1.add(txt_fin);
        txt_fin.setBounds(90, 40, 158, 20);

        cmb_comprobante.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Factura", "Nota de Venta", "Todos" }));
        cmb_comprobante.setName("cmb_comprobante"); // NOI18N
        jPanel1.add(cmb_comprobante);
        cmb_comprobante.setBounds(90, 70, 158, 20);

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton1);
        jButton1.setBounds(90, 90, 158, 39);

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N
        jPanel1.add(jLabel2);
        jLabel2.setBounds(20, 40, 68, 18);

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N
        jPanel1.add(jLabel1);
        jLabel1.setBounds(20, 10, 68, 23);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-333)/2, (screenSize.height-178)/2, 333, 178);
    }// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
//Sumo 1 dia a la fecha final por las horas:
    String DATE_FORMAT = "yyyy-MM-dd";//Refer Java DOCS for formats
    java.text.SimpleDateFormat sdf =  new java.text.SimpleDateFormat(DATE_FORMAT);
    
    Calendar c1 = Calendar.getInstance();
    String fechas[]=txt_fin.getText().split("-");
    c1.set(Integer.parseInt(fechas[0]),Integer.parseInt(fechas[1]),Integer.parseInt(fechas[2]));
    
    c1.add(Calendar.MONTH,-1);
    c1.add(Calendar.DAY_OF_MONTH,1);
   
    fecha2.setTime(c1.getTimeInMillis());
    
    if(txt_inicio.getText().equals("")){
            javax.swing.JOptionPane.showMessageDialog(this,"Ingresar Fecha de Inicio","Precaución",javax.swing.JOptionPane.WARNING_MESSAGE); 
        }else{
            if(txt_fin.getText().equals("")){
                javax.swing.JOptionPane.showMessageDialog(this,"Ingresar Fecha Fin","Precaución",javax.swing.JOptionPane.WARNING_MESSAGE); 
            }else{
                     if(txt_inicio.getText().compareTo(txt_fin.getText())<=0){
                         imprimir("Facturas Anuladas/"+"contenedor.jrxml");
                     }else{
                        javax.swing.JOptionPane.showMessageDialog(this,"La fecha fin no puede ser menor que la fecha de inicio","Precaución",javax.swing.JOptionPane.WARNING_MESSAGE); 
                     }
            }
        }
}//GEN-LAST:event_jButton1ActionPerformed

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
sacarFecha(1);
}//GEN-LAST:event_jButton2ActionPerformed

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
sacarFecha(2);
}//GEN-LAST:event_jButton3ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cmb_comprobante;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField txt_fin;
    private javax.swing.JTextField txt_inicio;
    // End of variables declaration//GEN-END:variables

}
